<?php

class Vehicle extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Mvehicle');
        $this->load->model('Mline');
    }

    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'vehicle/index_view';
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Vehicle[name]', 'Vehicle name', 'required');
            $this->form_validation->set_rules('Vehicle[capacity]', 'Capacity', 'required');
            if ($this->form_validation->run()) {
                $vehicle = $this->input->post('Vehicle');
                $this->Mvehicle->insertVehicle($vehicle);
                $this->session->set_flashdata('flash_mess', 'Create Vehicle Successfully.');
                redirect(base_url('vehicle/manage'));
            } else {
                $this->_data['data']['error'] = validation_errors('<li>','</li>');
            }
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function display($page="0") {
        $this->_data['load_page'] = 'vehicle/display_view';
        //Pagination   
        $config['base_url'] = base_url('vehicle/display/');
        $config['total_rows'] = $this->Mvehicle->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        $this->_data['data']['vehicle'] = $this->Mvehicle->listAllVehicles($config['per_page'], $page, 'id desc');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function manage($page="0") {
        //Pre-load View
        $this->_data['load_page'] = 'vehicle/manage_view'; 
        //Pagination   
        $config['base_url'] = base_url('vehicle/manage/');
        $config['total_rows'] = $this->Mvehicle->countAll();
        $config['per_page'] = 10; 
        $this->load->library('pagination', $config);
        $this->_data['pagination_links'] = $this->pagination->create_links();
        //Load View
        //echo '<pre>';
        //print_r($vehicle);echo'</pre>';exit();
        $this->_data['current_page'] = $page;
        $this->_data['data']['vehicle'] = $this->Mvehicle->listAllVehicles($config['per_page'], $page, 'id desc');  
        $this->_data['flash_mess'] = $this->session->flashdata('flash_mess');  
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function update($id) {
        //Pre-load View
        $this->_data['load_page']= 'vehicle/update_view';
        $this->_data['data'] = $this->Mvehicle->getVehicleById($id);
        $this->_data['data_id'] = $id;
        //Process
        if ($this->input->post('yt0')) {
            //Form Validation
            $this->load->library('form_validation');
            $this->form_validation->set_rules('Vehicle[name]', 'Vehicle name', 'required');
            if ($this->form_validation->run()) {
                $data_update = $this->input->post('Vehicle');
                $this->Mvehicle->updateVehicle($data_update, $id);
                $this->session->set_flashdata('flash_mess', 'Update Vehicle Successfully.');
                redirect(base_url('vehicle/manage'));
                exit();
            } else {
                $this->_data['data']['error'] = validation_errors('<li>','</li>');
            }  
        }
        //Load View        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function delete($id) {
        $this->Mvehicle->deleteVehicle($id);
        redirect(base_url('vehicle/manage'));
    }
    
    public function driver($id, $sid="") {
        //Pre-load View
        $this->_data['load_page'] = 'vehicle/setdriver_view';
        $this->load->model('Mdriver');
        $this->_data['data'] = $this->Mvehicle->getVehicleById($id);
        $this->_data['vehicle_driver'] = $this->Mdriver->listDriverVehicleById($id);
        //Process    
        $vehicle_id = $this->input->post('sltDriver');    
        if ($this->input->post('yt0')) {
            if (count($this->_data['vehicle_driver']) == 10) {
                $this->_data['data']['error'] = 'Exceeded Maximum allowed Driver.';
            } else {
                
                $this->Mdriver->setDriverVehicle($id, $vehicle_id);
                redirect(base_url('vehicle/driver/'.$id));
            }         
        }
        if ($sid != "") {
            $this->Mdriver->deleteDriverVehicle($sid);
            redirect(base_url('vehicle/driver/'.$id));
        }
        //Load View        
        $this->_data['data_id'] = $id;
        $this->_data['vehicle'] = $this->Mdriver->listAllDrivers();        
        $this->load->view($this->_data['template'], $this->_data);
    }
}

?>